<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>获取地理位置</title> 
 <style type="text/css">

     body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;}

    #allmap {width:100%;height:100%;}
 
  </style>
 <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=7a6QKaIilZftIMmKGAFLG7QT1GLfIncg"></script>
</head>

<body>
<!-- <p id="demo"></p>
   <p id="allmap"></p>
    <input type="button" onclick="mapclick()" value="确认" />
    <div id="position"></div>  -->
<td> <input type="text"  name="location" id="location" readonly="readonly"></input></td>
<button onclick="getLocation2()">点我</button>

<p id="demo" >点击按钮获取您当前坐标（可能需要比较长的时间获取：</p>
<p id="allmap"></p>
<script>
var x=document.getElementById("demo");

//获取经纬度，用百度API计算位置信息
function getLocation()
{
	if (navigator.geolocation)
	{
		navigator.geolocation.getCurrentPosition(showPosition,showError);
	}
	else
	{
		x.innerHTML="该浏览器不支持定位。";
	}
}
function showPosition(position)
{
	x.innerHTML="纬度: " + position.coords.latitude + 
	"<br>经度: " + position.coords.longitude;	
	
   var loc=document.getElementById("location");
   var map = new BMap.Map("allmap"); 
	var point = new BMap.Point(position.coords.longitude,position.coords.latitude); 
	map.centerAndZoom(point,12); 
	var geoc = new BMap.Geocoder(); 
	geoc.getLocation(point,function(rs){ 
	var addComp = rs.addressComponents; 
	 console.info(addComp);
	 alert(addComp.province + "," + addComp.city + "," + addComp.district +"," + addComp.street + "," + addComp.streetNumber); 
	//alert(addComp.province + "," + addComp.city + "," + addComp.district); 
	loc.value=addComp.province + "," + addComp.city + "," + addComp.district;
	}); 
 	
	
}
function showError(error)
{
	switch(error.code) 
	{
		case error.PERMISSION_DENIED:
			x.innerHTML="用户拒绝对获取地理位置的请求。"
			break;
		case error.POSITION_UNAVAILABLE:
			x.innerHTML="位置信息是不可用的。"
			break;
		case error.TIMEOUT:
			x.innerHTML="请求用户地理位置超时。"
			break;
		case error.UNKNOWN_ERROR:
			x.innerHTML="未知错误。"
			break;
	}
}

//方法二，chrome浏览器可用
function getLocation2() {
    // 创建百度地理位置实例，代替 navigator.geolocation
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function(e) {
    	 var loc=document.getElementById("location");
         var map = new BMap.Map("allmap"); 
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
            // 百度 geolocation 的经纬度属性不同，此处是 point.lat 而不是 coords.latitude
            x.innerHTML = '纬度：' + e.point.lat + '<br/>经度：' + e.point.lng;
           
           
         	var point = new BMap.Point(e.point.lng,e.point.lat); 
         	map.centerAndZoom(point,12); 
         	var geoc = new BMap.Geocoder(); 
         	geoc.getLocation(point,function(rs){ 
         	var addComp = rs.addressComponents; 
         	 console.info(addComp);
         	alert(addComp.province + "," + addComp.city + "," + addComp.district +"," + addComp.street + ","  + addComp.streetNumber); 
         	//alert(addComp.province + "," + addComp.city + "," + addComp.district); 
         	loc.value=addComp.province + "," + addComp.city + "," + addComp.district +"," + addComp.street + ","  + addComp.streetNumber;
         	});  
            
        } else {
            x.innerHTML = 'failed' + this.getStatus();
        }
    });
}

</script>
</body>
</html>